翻訳と辞書
Words near each other
・ Cocktail garnish
・ Cocktail glass
・ Cocktail hat
・ Cocktail hour (disambiguation)
・ Cocktail Hour (film)
・ Cocktail Kings
・ Cocktail Mixxx
・ Cocktail Molotov
・ Cocktail onion
・ Cocktail party
・ Cocktail party (disambiguation)
・ Cocktail party effect
・ Cocktail sauce
・ Cocktail shaker
・ Cocktail Soft
Cocktail sort
・ Cocktail stick
・ Cocktail Sticks
・ Cocktail strainer
・ Cocktail Time
・ Cocktail umbrella
・ Cocktail waitress
・ Cocktail Wars
・ Cocktails & Dreams
・ Cocktails (album)
・ Cocktails (film)
・ Cocktails (The Office)
・ Cocktails for Two
・ Cocktails with cachaça
・ Cocktales


Dictionary Lists
翻訳と辞書 辞書検索 [ 開発暫定版 ]
スポンサード リンク

Cocktail sort : ウィキペディア英語版
Cocktail sort

Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort,〔Martin Duhl: Die schrittweise Entwicklung und Beschreibung einer Shuffle-Sort-Array Schaltung in HYPERKARL aus der Algorithmischen Darstellung des BUBBLE-SORT-ALGORITHMUS, Projektarbeit, 1986, Technical University of Kaiserslautern〕 or shuttle sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts. It provides only marginal performance improvements, and does not improve asymptotic performance; like the bubble sort, it is not of practical interest (insertion sort is preferred for simple sorts), though it finds some use in education.
== Pseudocode ==
The simplest form of cocktail sort goes through the whole list each time:
procedure cocktailSort( A : list of sortable items ) defined as:
do
swapped := false
for each i in 0 to length( A ) - 2 do:
if A(i ) > A(i + 1 ) then // test whether the two elements are in the wrong order
swap( A(i ), A(i + 1 ) ) // let the two elements change places
swapped := true
end if
end for
if swapped = false then
// we can exit the outer loop here if no swaps occurred.
break do-while loop
end if
swapped := false
for each i in length( A ) - 2 to 0 do:
if A(i ) > A(i + 1 ) then
swap( A(i ), A(i + 1 ) )
swapped := true
end if
end for
while swapped // if no elements have been swapped, then the list is sorted
end procedure
The first rightward pass will shift the largest element to its correct place at the end, and the following leftward pass will shift the smallest element to its correct place at the beginning. The second complete pass will shift the second largest and second smallest elements to their correct places, and so on. After ''i'' passes, the first ''i'' and the last ''i'' elements in the list are in their correct positions, and do not need to be checked. By shortening the part of the list that is sorted each time, the number of operations can be halved (see bubble sort).
procedure cocktailSort( A : list of sortable items ) defined as:
// `begin` and `end` marks the first and last index to check
begin := -1
end := length( A ) - 2
do
swapped := false
// increases `begin` because the elements before `begin` are in correct order
begin := begin + 1
for each i in begin to end do:
if A(i ) > A(i + 1 ) then
swap( A(i ), A(i + 1 ) )
swapped := true
end if
end for
if swapped = false then
break do-while loop
end if
swapped := false
// decreases `end` because the elements after `end` are in correct order
end := end - 1
for each i in end to begin do:
if A(i ) > A(i + 1 ) then
swap( A(i ), A(i + 1 ) )
swapped := true
end if
end for
while swapped
end procedure
This is an example of the algorithm in MATLAB/OCTAVE.

function A = cocktailSort(A)
% `beginIdx` and `endIdx` marks the first and last index to check
beginIdx = 0;
endIdx = length(A)-1;
swapped = true;
while swapped
swapped = false;
% increases `beginIdx` because the elements before `beginIdx` are in correct order
beginIdx = beginIdx + 1;
for ii= beginIdx:endIdx
if A(ii) > A(ii + 1)
(A(ii) ) = deal(A(ii), A(ii+1));
swapped = true;
end
end
if ~swapped
break;
end
swapped = false;
% decreases `endIdx` because the elements after `endIdx` are in correct order
endIdx = endIdx - 1;
for ii= endIdx:-1:beginIdx
if A(ii) > A(ii + 1)
(A(ii) ) = deal(A(ii), A(ii+1));
swapped = true;
end
end
end
end


抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)
ウィキペディアで「Cocktail sort」の詳細全文を読む



スポンサード リンク
翻訳と辞書 : 翻訳のためのインターネットリソース

Copyright(C) kotoba.ne.jp 1997-2016. All Rights Reserved.